home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 2 / ETO Development Tools 2.iso / Essentials / Developer Essentials Nov 90 / Apple II / Apple.II.partition / Tools / DTS.Samples / SC17Darts / Darts.p / UWindow.inc.p < prev    next >
Encoding:
Text File  |  1990-06-25  |  12.4 KB  |  356 lines  |  [TEXT/pdos]

  1. {**********************************************************************
  2. {*
  3. {* Darts uWindow -- Version 3.0  (implementation)
  4. {*
  5. {* Copyright (c)
  6. {* Apple Computer, Inc.  1986-1989
  7. {* All Rights Reserved.
  8. {*
  9. {* Developer Technical Support Apple II Sample Code
  10. {*
  11. {* This file contains the code which implements  
  12. {* windows in the program.
  13. {*
  14. {**********************************************************************}
  15. {$R-}
  16.  
  17. {**********************************************************************
  18. {*
  19. {* InvalScore
  20. {*
  21. {* Invalidates a rectangle that holds the score for player one or two
  22. {* so that it will be updated.
  23. {*
  24. {**********************************************************************}
  25. procedure   InvalScore (PlayerNum : integer);
  26.     var
  27.         tempHandle : CtlRecHndl;
  28.         id : longint;
  29.         keepPort: GrafPortPtr;
  30.         
  31.     begin
  32.         id := $60000;
  33.         if PlayerNum = Player1 then id := $50000;
  34.         tempHandle := GetCtlHandleFromID(theWindow,id);
  35.         
  36.         keepPort := GetPort;
  37.         SetPort(theWindow);
  38.         InvalRect (tempHandle^^.CtlRect);
  39.         SetPort(keepPort);
  40.     end;
  41.  
  42. {**********************************************************************
  43. {*
  44. {* drawThisWindow
  45. {*
  46. {* Draws the window whose port is the current port.
  47. {*
  48. {**********************************************************************}
  49. procedure drawThisWindow;
  50.  
  51.  
  52.     var
  53.         tempRect : rect;
  54.         
  55.     begin
  56.         firstUpdateComplete := true;    
  57.  
  58.         if GameType = 0 then
  59.             begin
  60.                 GetPortRect (TempRect);
  61.                 tempRect.Left := tempRect.Left+(tempRect.Right-tempRect.Left) div 2;
  62.                 tempRect.Right := tempRect.Left+2;
  63.                 tempRect.Top := tempRect.Top+15;
  64.                 tempRect.Bottom := tempRect.bottom-18;
  65.         
  66.                 PaintRect(tempRect);
  67.             end;
  68.  
  69.  
  70.         ScoreStrings[Player1] := IntToString(Score[Player1]);
  71.         ScoreStrings[Player2] := IntToString(Score[Player2]);
  72.  
  73.         SetCtlParamPtr(@scoreStringArray);
  74.  
  75.         DrawControls(TheWindow);
  76.         
  77.     END;
  78.  
  79.  
  80.  
  81. {**********************************************************************
  82. {*
  83. {* drawListItem
  84. {*
  85. {* Draws the indicated list item.  
  86. {*
  87. {**********************************************************************}
  88. procedure drawListItem  (var r : rect; myMemRecPtr : theMemRecPtr; listControl : Handle);
  89.     var
  90.         s : str255;
  91.         theScore,
  92.         mode: integer;
  93.         selected: boolean;              
  94.  
  95.     begin
  96.         
  97.         theScore := integer(myMemRecPtr^.MemPtr);
  98.         s := IntToString(TheScore);
  99.         selected := (BAND(myMemRecPtr^.memFlag,$C0) = $80);
  100.  
  101.         mode := GetPenMode;
  102.         if not selected then SetPenMode($8000);
  103.         PaintRect(r);
  104.         SetPenMode(mode);
  105.  
  106.         
  107.         MoveTo(r.left+4,r.bottom-1);
  108.  
  109.         mode := GetTextMode;
  110.         SetTextMode($8002);
  111.         DrawString(s);
  112.         SetTextMode(mode);
  113.     end;
  114.  
  115.  
  116.  
  117. {**********************************************************************
  118. {*
  119. {* addToList
  120. {*
  121. {* Adds the indicated score to the right list.  
  122. {*
  123. {**********************************************************************}
  124. procedure   addToList       (playerNum : integer; amount : integer);
  125.     var
  126.         TopOfList : integer;
  127.         TheHandle : ListCtlRecHndl;
  128.         
  129.     begin
  130.         ListEntries[PlayerNum] := ListEntries[PlayerNum]+1;
  131.         
  132.         TopOfList := ListEntries[PlayerNum] - 3;
  133.         if TopOfList < 1 then TopOfList := 1;
  134.         
  135.         If ListEntries[PlayerNum]*sizeof(theMemRec) >= gethandlesize (handle(ScoreList[PlayerNum])) then
  136.             SetHandleSize((100+ListEntries[PlayerNum])*sizeOf(theMemRec),handle(ScoreList[PlayerNum]));
  137.         
  138.         ScoreList[PlayerNum]^^[ListEntries[PlayerNum]-1].MemPtr := ptr(Amount);
  139.         ScoreList[PlayerNum]^^[ListEntries[PlayerNum]-1].MemFlag := 0;
  140.         
  141.         if PlayerNum = 1 then
  142.             TheHandle := ListCtlRecHndl(GetCtlHandleFromID(TheWindow,$30000))
  143.         else
  144.             TheHandle := ListCtlRecHndl(GetCtlHandleFromID(TheWindow,$40000));
  145.         NewList2    (@DrawListItem,                         { drawing routine addr }
  146.                      TopOfList,                             { First Item to display }
  147.                      Ref(ScoreList[PlayerNum]),             { ref to mem rec array }
  148.                      RefIsHandle,                           { List type }
  149.                      ListEntries[PlayerNum],                { Num Items }
  150.                      CtlRecHndl(TheHandle));                            { List ctl handle }
  151. end;
  152.     
  153. {*********************************************************************************}
  154. {*
  155. {* FixButtonTitle
  156. {*
  157. {* This routine is called only when the chosen game is crickett.  Its job
  158. {* is to adjust the button title of the button associated with the amount
  159. {* passed.
  160. {*
  161. {* There are four possible states of the button title.  Which state it is
  162. {* in depends on the number times it has been pressed (contained in the
  163. {* CrickettTables).
  164. {*
  165. {**********************************************************************************}
  166. procedure   FixButtonTitle  (PlayerNum : integer; Amount : integer);
  167.     var
  168.         TheHandle : CtlRecHndl;
  169.         TheID : LongInt;
  170.  
  171.     begin
  172.         if PlayerNum = Player1 
  173.             then TheID := Amount
  174.             else TheID := $00008000 + Amount;
  175.             
  176.         theHandle := GetCtlHandleFromID(theWindow,theID);
  177.         case crickettTables[playerNum][amount] of
  178.             0:  SetCtlTitle(BlankStr,TheHandle);
  179.             1:  SetCtlTitle(OneStr,TheHandle);
  180.             2:  SetCtlTitle(TwoStr,TheHandle);
  181.             otherwise   SetCtlTitle(ThreeStr,TheHandle);
  182.         end; {case}
  183.     end;
  184.     
  185.     
  186.     
  187. {**********************************************************************
  188. {*
  189. {* RemoveSelected
  190. {*
  191. {* Removes any selected scores from the list.
  192. {*
  193. {**********************************************************************}
  194. procedure   RemoveSelected      (PlayerNum : integer);
  195.     
  196.     var
  197.         K : integer;
  198.         ItemToRemove : integer;
  199.         TheHandle : ListCtlRecHndl;
  200.         Amount : integer;
  201.         TopOfList : integer;
  202.     
  203.     begin
  204.     
  205.         { Use PlayerNum to get handle to list control }
  206.         if PlayerNum = Player1 then
  207.             TheHandle := ListCtlRecHndl(GetCtlHandleFromID(TheWindow,$30000))
  208.         else
  209.             TheHandle := ListCtlRecHndl(GetCtlHandleFromID(TheWindow,$40000));
  210.  
  211.         repeat
  212.             ItemToRemove := NextMember2(0,CtlRecHndl(TheHandle));
  213.             if ItemToRemove <> 0 then
  214.                 begin
  215.                     { Value to remove is...}
  216.                     amount := LongInt(scoreList[playerNum]^^[itemToRemove-1].memPtr);
  217.  
  218.  
  219.                     { Do it a little different depending on the game. }
  220.                     if GameType = 0 then
  221.                         Score[PlayerNum] := Score[PlayerNum] - Amount
  222.                     else 
  223.                         begin
  224.                             if CrickettTables[PlayerNum][Amount] > 3 then 
  225.                                 Score[PlayerNum] := Score[PlayerNum]-Amount;
  226.                             CrickettTables[PlayerNum][Amount] := CrickettTables[PlayerNum][Amount] - 1;
  227.                             FixButtonTitle(PlayerNum,Amount);
  228.                         end;
  229.  
  230.  
  231.                     { In all cases, we remove the item from the list and redisplay. }
  232.                     for k := ItemToRemove to ListEntries[PlayerNum]-1 do
  233.                         begin
  234.                             ScoreList[PlayerNum]^^[k-1].MemPtr := ScoreList[PlayerNum]^^[k].MemPtr;
  235.                             ScoreList[PlayerNum]^^[k-1].MemFlag := ScoreList[PlayerNum]^^[k].MemFlag;
  236.                         end;
  237.                     ListEntries[PlayerNum] := ListEntries[PlayerNum] -1;
  238.                     
  239.                     TopOfList := GetCtlValue(CtlRecHndl(TheHandle))-1;
  240.                     If TopOfList < 1 then TopOfList := 1;
  241.                     
  242.                     NewList2    (@DrawListItem,                             { drawing routine addr }
  243.                                  TopOfList,                                 { First Item to display }
  244.                                  Ref(ScoreList[PlayerNum]),                 { ref to mem rec array }
  245.                                  RefIsHandle,                               { List type }
  246.                                  ListEntries[PlayerNum],                    { Num Items }
  247.                                  CtlRecHndl(TheHandle));                    { List ctl handle }
  248.         
  249.                 end;
  250.         until ItemToRemove = 0;
  251.         InvalScore(PlayerNum);
  252.     end;
  253.  
  254.  
  255.  
  256. {**********************************************************************
  257. {*
  258. {* ClearList
  259. {*
  260. {* Clears the indicated list.  
  261. {*
  262. {**********************************************************************}
  263. procedure   ClearList       (PlayerNum : integer);
  264.     var
  265.         TheHandle : ListCtlRecHndl;
  266.     begin
  267.         if PlayerNum = Player1 then
  268.             TheHandle := ListCtlRecHndl(GetCtlHandleFromID(TheWindow,$30000))
  269.         else
  270.             TheHandle := ListCtlRecHndl(GetCtlHandleFromID(TheWindow,$40000));
  271.  
  272.         ListEntries[PlayerNum] := 0;
  273.         
  274.         NewList2    (@DrawListItem,                         { drawing routine addr }
  275.                      0,                                     { Item to display }
  276.                      Ref(ScoreList[PlayerNum]),             { ref to mem rec array }
  277.                      RefIsHandle,                           { List type }
  278.                      0,                                     { Num Items }
  279.                      CtlRecHndl(TheHandle));                { List ctl handle }
  280.     end;
  281.  
  282. {**********************************************************************************}
  283. {*
  284. {* StartUpRobinGame
  285. {*
  286. {* Opens the robin game window and zeros the scores.
  287. {*
  288. {**********************************************************************************}
  289. procedure StartupRobinGame;
  290.     begin
  291.         TheWindow := NewWindow2     (   NIL,                                { Ref to title }
  292.                                         1,                                      { Ref Con }
  293.                                         @DrawThisWindow,                        { Draw routine }
  294.                                         NIL,                                    { DefProc Ptr }
  295.                                         RefIsResource,                          { Ref Descriptor }
  296.                                         REF(RobinWindow),                       { Input Ref }
  297.                                         rWindParam1);                           { Resource Type }
  298.  
  299.  
  300.         ListEntries[Player1] := 0;
  301.         ListEntries[Player2] := 0;
  302.         gameType := 0;
  303.     end;
  304.  
  305.  
  306. {**********************************************************************************}
  307. {*
  308. {* StartupCrickettGame
  309. {*
  310. {* Opens the crickett game window and zeros the scores.
  311. {*
  312. {**********************************************************************************}
  313. procedure StartupCrickettGame;
  314.     begin
  315.         TheWindow := NewWindow2     (   NIL,                                { Ref to title }
  316.                                         1,                                      { Ref Con }
  317.                                         @DrawThisWindow,                        { Draw routine }
  318.                                         NIL,                                    { DefProc Ptr }
  319.                                         RefIsResource,                          { Ref Descriptor }
  320.                                         REF(CrickettWindow),                        { Input Ref }
  321.                                         rWindParam1);                           { Resource Type }
  322.  
  323.  
  324.         ListEntries[Player1] := 0;
  325.         ListEntries[Player2] := 0;
  326.         WeHaveAWinner := false;
  327.         gameType := 1;
  328.     end;
  329.     
  330.     
  331. {**********************************************************************
  332. {*
  333. {* SetupWindows
  334. {*
  335. {* Inits any of the window unit variables we need to start up 
  336. {* the game.  Calls the routine that opens the first window.
  337. {*
  338. {**********************************************************************}
  339. procedure SetUpWindows;
  340.     const
  341.         InitListSize = 100;
  342.         
  343.     begin   {of SetUpWindows}
  344.         FirstUpdateComplete := false;   
  345.  
  346.         ScoreList[Player1] := listArrayHandle(newhandle(sizeof(theMemRec)*InitListSize,userID,0,NIL));
  347.         ScoreList[Player2] := listArrayHandle(newhandle(sizeof(theMemRec)*InitListSize,userID,0,NIL));
  348.  
  349.         ListEntries[Player1] := 0;
  350.         ListEntries[Player2] := 0;
  351.  
  352.         StartupRobinGame;
  353.  
  354.     end;    {of SetUpWindows}
  355.     
  356.